package cn.edu.gsau.info.service.impl;

import cn.edu.gsau.info.dao.ZhuanlizhuanzhuDao;
import cn.edu.gsau.info.models.Shenhe;
import cn.edu.gsau.info.models.User;
import cn.edu.gsau.info.models.Zhengmingcailiao;
import cn.edu.gsau.info.models.Zhuanlizhuanzhu;
import cn.edu.gsau.info.myenum.ModuleIdClass;
import cn.edu.gsau.info.service.*;
import cn.edu.gsau.info.vo.FileList;
import cn.edu.gsau.info.vo.Review;
import cn.edu.gsau.info.vo.ZhuanlizhuanzhuRules;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * @BelongsProject: ZhuanlizhuanzhuServiceImpl
 * @BelongsPackage: cn.edu.gsau.info.service.impl
 * @Author: qsy
 * @CreateTime: 2022/11/18
 * @Description:
 * @Version: 1.0
 */
@Service
public class ZhuanlizhuanzhuServiceImpl extends ServiceImpl<ZhuanlizhuanzhuDao, Zhuanlizhuanzhu> implements IZhuanlizhuanzhuService {
    @Autowired
    ZhuanlizhuanzhuDao zhuanlizhuanzhuDao;
    @Autowired
    IZhengmingcailiaoService iZhengmingcailiaoService;

    @Autowired
    IShenheService iShenHeService;

    @Autowired
    IReviewService iReviewService;

    @Autowired
    IUserService iUserServicel;

    /**
     * @Description 通过填写人id和流程状态搜索分页查询专利专著
     * @Param [ruler]
     * @return com.baomidou.mybatisplus.core.metadata.IPage
     * @Date 2022/11/25 15:07
     * @Author qsy
     **/
    @Override
    public IPage selectZhuanlizhuanzhuByStatus(ZhuanlizhuanzhuRules rules){
        IPage<Zhuanlizhuanzhu> page=new Page<>(rules.getPageIndex(),rules.getPageSize());
        QueryWrapper<Zhuanlizhuanzhu> qw=new QueryWrapper<>();
        qw.eq("tianbaoId",rules.getTianbaoId());
        qw.eq(rules.getStatus()!=null&&rules.getStatus()!="","status",rules.getStatus());
        qw.like(rules.getName()!=null&&rules.getName()!="","name",rules.getName());
        IPage iPage=zhuanlizhuanzhuDao.selectPage(page,qw);
        return iPage;
    }
    /**
     * @Description 通过专利专著id修改单条专利专著和证明材料,并查询新的专利专著和证明材料
     * @Param [user,form,id]
     * @return cn.edu.gsau.info.models.Zhuanlizhuanzhu
     * @Date 2022/11/27 14:20
     * @Author qsy
     **/
    @Override
    public Zhuanlizhuanzhu updateInfo(User user, Zhuanlizhuanzhu form, long id){
        QueryWrapper<Zhuanlizhuanzhu> qw=new QueryWrapper<>();
        qw.eq("id",id);
        form.setUpdateBy(user.getNickName());
        form.setNode("待审核");
        int update=zhuanlizhuanzhuDao.update(form,qw);
        System.out.println(form.getFileList());
        if (form.getFileList()!=null){
            FileList fileList[]=form.getFileList();
            for (int i = 0; i <fileList.length ; i++) {
                Zhengmingcailiao zhengmingcailiao =new Zhengmingcailiao();
                zhengmingcailiao.setModuleId(ModuleIdClass.ZHUANLIYURUANJIAN_id);
                zhengmingcailiao.setRecoreId(form.getId());
                zhengmingcailiao.setFileName(fileList[i].getOldFileName());
                zhengmingcailiao.setFileFullPath(user.getUserName()+ "/" + fileList[i].getNewFileName());
                boolean reselut =iZhengmingcailiaoService.save(zhengmingcailiao);
            }
        }
        Date date=new Date();
        Shenhe shenHe=new Shenhe();
        shenHe.setModuleId(ModuleIdClass.ZHUANLIYURUANJIAN_id);
        shenHe.setTijiaoTime(date);
        shenHe.setRecoreId(form.getId());
        shenHe.setTijiaoId(user.getId());
        shenHe.setTijiaoName(user.getNickName());
        shenHe.setTijiaoMemo(form.getMemo());
        shenHe.setShenHeResult("待审核");
        boolean shenhe=iShenHeService.save(shenHe);
        Zhuanlizhuanzhu zhuanlizhuanzhu=zhuanlizhuanzhuDao.selectById(id);
        return update>0?zhuanlizhuanzhu:null;
    }
    /**
     * @Description 通过用户id添加专利专著、证明材料以及审核流程
     * @Param [user,form]
     * @return boolean
     * @Date 2022/11/26 14:20
     * @Author qsy
     **/
    @Override
    public boolean addInfo(User user,Zhuanlizhuanzhu form){
        QueryWrapper qw=new QueryWrapper();
        qw.eq("moduleId",ModuleIdClass.ZHUANLIYURUANJIAN_id);
        Review review=iReviewService.getOne(qw);
        User shenheUser=iUserServicel.getById(review.getUserId());

        Date date=new Date();
        form.setCreateTime(date);
        form.setTianbaoId(user.getId());
        form.setCreateBy(user.getNickName());
        form.setManager(shenheUser.getNickName());
        form.setDeleted(0);
        form.setStatus("进行中");
        form.setNode("待审核");
        int add=zhuanlizhuanzhuDao.insert(form);

        Shenhe shenHe=new Shenhe();
        shenHe.setModuleId(ModuleIdClass.ZHUANLIYURUANJIAN_id);
        shenHe.setTijiaoTime(date);
        shenHe.setRecoreId(form.getId());
        shenHe.setTijiaoId(user.getId());
        shenHe.setTijiaoName(user.getNickName());
        shenHe.setTijiaoMemo(form.getMemo());
        shenHe.setShenHeResult("待审核");
        boolean shenhe=iShenHeService.save(shenHe);

        FileList fileList[]=form.getFileList();
        for (int i = 0; i <fileList.length; i++) {
            Zhengmingcailiao zhengmingcailiao = new Zhengmingcailiao();
            zhengmingcailiao.setModuleId(ModuleIdClass.ZHUANLIYURUANJIAN_id);
            zhengmingcailiao.setRecoreId(form.getId());
            zhengmingcailiao.setFileName(fileList[i].getOldFileName());
            System.out.println(fileList[i].getNewFileName());
            zhengmingcailiao.setFileFullPath(user.getUserName() + "/" + fileList[i].getNewFileName());
            boolean reslut=iZhengmingcailiaoService.save(zhengmingcailiao);
        }
        return add>0&&shenhe==true?true:false;
    }

    /**
     * @Description 通过填写人id和节点搜索分页查询专利专著
     * @Param [ruler]
     * @return com.baomidou.mybatisplus.core.metadata.IPage
     * @Date 2022/11/25 15:07
     * @Author qsy
     **/
    @Override
    public IPage selectZhuanlizhuanzhuByNode(ZhuanlizhuanzhuRules rules){
        IPage<Zhuanlizhuanzhu> page=new Page<>(rules.getPageIndex(),rules.getPageSize());
        QueryWrapper<Zhuanlizhuanzhu> qw=new QueryWrapper<>();
        qw.eq("tianbaoId",rules.getTianbaoId());
        qw.eq(rules.getNode()!=null&&rules.getNode()!="","node",rules.getNode());
        qw.like(rules.getName()!=null&&rules.getName()!="","name",rules.getName());
        IPage iPage=zhuanlizhuanzhuDao.selectPage(page,qw);
        return iPage;
    }

    /**
     * @Description 添加审核流程
     * @Param [shenHe]
     * @return boolean
     * @Date 2022/11/30 10:03
     * @Author qsy
     **/
    @Override
    public boolean addShenHe(Shenhe shenHe){
        QueryWrapper qw=new QueryWrapper();
        qw.eq("moduleId",ModuleIdClass.ZHUANLIYURUANJIAN_id);
        Review review=iReviewService.getOne(qw);
        User shenheUser=iUserServicel.getById(review.getUserId());

        Date date=new Date();
        shenHe.setShenHeTime(date);
        shenHe.setShenHeName(shenheUser.getNickName());
        shenHe.setShenheId(shenheUser.getId());
        boolean result=iShenHeService.addShenHe(shenHe);
        Zhuanlizhuanzhu zhuanlizhuanzhu=new Zhuanlizhuanzhu();
        if(shenHe.getShenHeResult().equals("审核已通过")){
            zhuanlizhuanzhu.setStatus("流转完成");
        }
        zhuanlizhuanzhu.setNode(shenHe.getShenHeResult());
        QueryWrapper qw1=new QueryWrapper();
        qw.eq("id",shenHe.getRecoreId());
        int i=zhuanlizhuanzhuDao.update(zhuanlizhuanzhu,qw1);
        return result==true&&i>0?true:false;
    }

    /**
     * @Description 根据项目id查询最后的审核记录
     * @Param [recoreId]
     * @return cn.edu.gsau.info.models.ShenHe;
     * @Date 2022/11/29 19:50
     * @Author qsy
     **/

    @Override
    public Shenhe findEndShenHe(long recoreId){
        Shenhe shenHe=iShenHeService.findEndShenHe(recoreId);
        return shenHe;
    }
}
